package com.yaolaizaixian.merchant.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yaolaizaixian.merchant.R;
import com.yaolaizaixian.merchant.bean.SalesShopData;

import java.util.List;

/**
 * Created by ylzx on 2017/2/27.
 *
 */
public class FragmentSalesShopAdapter extends RecyclerView.Adapter<FragmentSalesShopAdapter.ViewHolder> {


    public List<SalesShopData> datas;

    private Context mcontext;

    private OnItemClickListener mOnItemClickListener;

    public FragmentSalesShopAdapter(Context context, List<SalesShopData> datas) {

        this.datas = datas;
        this.mcontext = context;

    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_salesshop, viewGroup, false);
        return new ViewHolder(view);
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        SalesShopData salesShopData = datas.get(position);

        viewHolder.tvSalesshopName.setText(salesShopData.getUserName());
        viewHolder.tvSalesshopMoney.setText("￥"+salesShopData.getTotalAmt());
        if (position == 0) {
            viewHolder.ivSalesshopImage.setImageResource(R.mipmap.paihangbang_yi);
            viewHolder.tv_salesshop_paiming.setText("");
        } else if (position == 1) {
            viewHolder.ivSalesshopImage.setImageResource(R.mipmap.paihangbang_er);
            viewHolder.tv_salesshop_paiming.setText("");
        } else if (position == 2) {
            viewHolder.ivSalesshopImage.setImageResource(R.mipmap.paihangbang_san);
            viewHolder.tv_salesshop_paiming.setText("");
        } else {
            viewHolder.ivSalesshopImage.setImageResource(R.mipmap.paihangbang_liu);
            int number = position + 1;
            viewHolder.tv_salesshop_paiming.setText(number + "");
        }

        if (mOnItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    mOnItemClickListener.onClick(position);
                }
            });

            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    mOnItemClickListener.onLongClick(position);
                    return false;
                }
            });
        }
    }


    //获取数据的数量
    @Override
    public int getItemCount() {

        return datas.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivSalesshopImage;
        TextView tvSalesshopName;
        TextView tvSalesshopMoney;
        TextView tv_salesshop_paiming;

        public ViewHolder(View view) {

            super(view);
            ivSalesshopImage = (ImageView) view.findViewById(R.id.iv_salesshop_image);
            tvSalesshopName = (TextView) view.findViewById(R.id.tv_salesshop_name);
            tvSalesshopMoney = (TextView) view.findViewById(R.id.tv_salesshop_money);
            tv_salesshop_paiming = (TextView) view.findViewById(R.id.tv_salesshop_paiming);

        }
    }

    public interface OnItemClickListener {
        void onClick(int position);

        void onLongClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {

        this.mOnItemClickListener = onItemClickListener;

    }

}

